package com.limin.commanser.controller;

import com.github.pagehelper.PageHelper;
import com.limin.commanser.dto.ExpenseDto;
import com.limin.commanser.entity.Expense;
import com.limin.commanser.result.Result;
import com.limin.commanser.result.ResultGenerator;
import com.limin.commanser.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ExpenseController {
    private final ExpenseService expenseService;

    @Autowired
    ExpenseController(ExpenseService expenseService) {
        this.expenseService = expenseService;
    }

    @GetMapping("/expenses")
    public Result findAllByPaging(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam("userId") Integer userId,
                                  @RequestParam(value = "order", required = false, defaultValue = "DESC") String order,
                                  @RequestParam(value = "activityStatus",required = false) Integer activityStatus) {
        PageHelper.startPage(pageNum, pageSize);
        Map<Object, Object> params = new HashMap<>();
        params.put("order", order);
        params.put("activityStatus", activityStatus);
        params.put("userId",userId);
        List<ExpenseDto> expenseDtos = expenseService.findAllByPaging(userId);
        return ResultGenerator.genOkResult(expenseDtos);
    }

    @GetMapping("/expenses/findAll")
    public Result findAllByPagingAll(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam(value = "order", required = false, defaultValue = "DESC") String order,
                                  @RequestParam(value = "activityStatus",required = false) Integer activityStatus) {
        PageHelper.startPage(pageNum, pageSize);
        Map<Object, Object> params = new HashMap<>();
        params.put("order", order);
        params.put("activityStatus", activityStatus);
        List<ExpenseDto> expenseDtos = expenseService.findAllByPagingAll(params);
        return ResultGenerator.genOkResult(expenseDtos);
    }

    @GetMapping("/expenses/total")
    public Result findTotal() {
        return ResultGenerator.genOkResult(expenseService.findTotal());
    }

    @PostMapping("/expenses")
    public Result add(@RequestBody Expense expense) {
        expenseService.add(expense);
        return ResultGenerator.genOkResult("ok");
    }

    @PutMapping("/expenses/approval")
    public Result approvalExpense(@RequestBody Expense expense) {
        expenseService.approvalExpense(expense);
        return ResultGenerator.genOkResult("ok");
    }

    @DeleteMapping("/expenses/{id}")
    public Result removeById(@PathVariable Integer id) {
        expenseService.removeById(id);
        return ResultGenerator.genOkResult("ok");
    }

    @PostMapping("/expenseUpdateInfo")
    public Result expenseUpdateInfo(@RequestBody Expense expense) {
        expenseService.update(expense);
        return ResultGenerator.genOkResult();
    }

    @PostMapping("/batchDeleteById")
    public Result batchDeleteById(@RequestBody String[] ids) {
        return ResultGenerator.genOkResult(expenseService.batchDeleteById(ids));
    }

    @GetMapping("/expenses/like/{string}")
    public Result findAllByLike(@PathVariable("string") String string) {
        return ResultGenerator.genOkResult(expenseService.findAllByLike(string));
    }
}
